package com.haoze.nurse.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.haoze.common.response.Result;
import com.haoze.common.response.ResultGenerator;
import com.haoze.common.utils.UUIDUtil;
import com.haoze.nurse.dao.NurseConsultationGroupMapper;
import com.haoze.nurse.enumeration.NurseEnum;
import com.haoze.nurse.model.NurseConsultationGroupEntity;
import com.haoze.nurse.service.ConsultationGroupService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Condition;
import tk.mybatis.mapper.entity.Example;

import java.util.List;
import java.util.Map;

/**
 * @Author: Chenchao
 * @CreateTime: 2020-12-22 10:35
 */
@Service
public class ConsultationGroupServiceImpl implements ConsultationGroupService {

    @Autowired
    private NurseConsultationGroupMapper nurseConsultationGroupMapper;


    @Override
    public List<Map<String, String>> queryNurse(String queryString, String dataCode, int page, int pageSize) {
        return nurseConsultationGroupMapper.queryNurse(queryString, dataCode, page, pageSize);
    }


    @Override
    public Result addGroupNurse(NurseConsultationGroupEntity entity) {
        Condition condition = new Condition(NurseConsultationGroupEntity.class);
        Example.Criteria criteria = condition.createCriteria();
        criteria.andEqualTo("userId", entity.getUserId());
        criteria.andEqualTo("delFlag", NurseEnum.DEL_FLAG_ZERO.getIndex());
        int count = nurseConsultationGroupMapper.selectCountByCondition(condition);
        if (count > 0) {
            return ResultGenerator.genFailedResult("该护士已添加，请勿重复添加");
        }
        if (NurseEnum.GROUP_ROLE_ONE.getIndex().equals(entity.getRole())) {
            Condition condition2 = new Condition(NurseConsultationGroupEntity.class);
            Example.Criteria criteria2 = condition2.createCriteria();
            criteria2.andEqualTo("role", entity.getRole());
            criteria2.andEqualTo("area", entity.getArea());
            criteria2.andEqualTo("delFlag", NurseEnum.DEL_FLAG_ZERO.getIndex());
            int count2 = nurseConsultationGroupMapper.selectCountByCondition(condition2);
            if (count2 > 0) {
                return ResultGenerator.genFailedResult("该病区已有组长，请勿重复添加");
            }
        }
        entity.initAdd();
        entity.setConsultaionGroupId(UUIDUtil.randomString());
        entity.setDelFlag(NurseEnum.DEL_FLAG_ZERO.getIndex());
        nurseConsultationGroupMapper.insert(entity);
        return ResultGenerator.genOkResult();
    }

    @Override
    public Result updateGroupNurse(NurseConsultationGroupEntity entity) {
        if (NurseEnum.GROUP_ROLE_ONE.getIndex().equals(entity.getRole())) {
            Condition condition = new Condition(NurseConsultationGroupEntity.class);
            Example.Criteria criteria = condition.createCriteria();
            criteria.andEqualTo("delFlag", NurseEnum.DEL_FLAG_ZERO.getIndex());
            criteria.andEqualTo("groupName", entity.getGroupName());
            criteria.andEqualTo("role", entity.getRole());
            criteria.andEqualTo("area", entity.getArea());
            criteria.andNotEqualTo("userId",entity.getUserId());
            int count = nurseConsultationGroupMapper.selectCountByCondition(condition);
            if (count > 0) {
                return ResultGenerator.genFailedResult("当前病区已有组长！");
            }
            entity.initUpdate();
            nurseConsultationGroupMapper.updateByPrimaryKey(entity);
        } else {
            nurseConsultationGroupMapper.updateByPrimaryKey(entity);
        }
        return ResultGenerator.genOkResult();
    }

    @Override
    public Result delGroupNurse(String consultaionGroupId) {
        NurseConsultationGroupEntity entity = nurseConsultationGroupMapper.selectByPrimaryKey(consultaionGroupId);
        entity.setDelFlag(NurseEnum.DEL_FLAG_ONE.getIndex());
        entity.initUpdate();
        nurseConsultationGroupMapper.updateByPrimaryKey(entity);
        return ResultGenerator.genOkResult();
    }

    @Override
    public Result groupNurseList(String queryString, int page, int pageSize) {
        PageHelper.startPage(page, pageSize);
        List<Map<String, String>> result = nurseConsultationGroupMapper.groupNurseList(queryString);
        PageInfo<Map<String, String>> pageInfo = new PageInfo<>(result);
        return ResultGenerator.genOkResult(pageInfo);
    }


}
